#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
//栈和队列的顺序存储都有一个顶指针指向最后一个有效
//结点的下一个结点
//他们的链式存储和链表一样都有一个头结点

typedef struct QNode /* 声明链式队列的结点 */
{
    int data;
    struct QNode *pNext;

} NODE, *PNODE;

typedef struct Queue /* 声明链式队列的首尾指针 */
{
    PNODE front; //队头指针
    PNODE rear;  //队尾指针
} QUEUE, *PQUEUE;

//初始化
void init(PQUEUE lQ)
{
    lQ->front = lQ->rear = (PNODE)malloc(sizeof(NODE) * MAXSIZE);
    if (lQ->front == NULL)
    {
        printf("初始化失败");
        return;
    }
    lQ->front->pNext = NULL;
}

//销毁
void destoryQueue(PQUEUE lQ)
{
    PQUEUE p;
    while (lQ->front)
    {
        p = lQ->front->pNext;
        free(lQ->front);
        lQ->front = p;
    }
}

//入队
void enterQueue(PQUEUE lQ, int data)
{
    PNODE pNew = (PNODE)malloc(sizeof(NODE));
    pNew->data = data;
    pNew->pNext = NULL;
    lQ->rear->pNext = pNew;
    //将rear指向最后一个
    lQ->rear = pNew;
}

//出队
void deleteQUEUE(PQUEUE lQ, int *e)
{
    if (lQ->front == lQ->rear)
    {
        printf("队伍为空");
        return;
    }
    PNODE p = lQ->front->pNext;
    *e = p->data;
    lQ->front->pNext = p->pNext;
    if (p == lQ->rear)
    {
        lQ->rear = lQ->front;
    }
    free(p);
    p = NULL;
}

//取队头元素
void getHead(PQUEUE lQ, int *eHead)
{
    if (lQ->front == lQ->rear)
    {
        printf("队伍为空");
        return;
    }
    *eHead = lQ->front->pNext->data;
}

int main()
{
    int *eHead;
    int *e;
    QUEUE lQ;
    init(&lQ);
    enterQueue(&lQ, 2);
    enterQueue(&lQ, 1);

    deleteQUEUE(&lQ, &e);
    printf("出队元素是%d", e);
    getHead(&lQ, &eHead);
    printf("队头元素是%d", eHead);
}